home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Palm Finder 2 / Src / Panes / superclock.cpp < prev    next >
Encoding:
Text File  |  2001-06-23  |  2.5 KB  |  140 lines

  1. // superclock.cpp
  2.  
  3.  
  4. // includes
  5. #include "superclock.h"
  6. #include "drawing.h"
  7. #include "events.h"
  8.  
  9.  
  10. // constants
  11. const UInt32        k_update_interval = 2; // seconds
  12. const UInt32        k_date_interval = 5; // seconds
  13.  
  14.  
  15. //
  16. // constructor
  17. //
  18. superclock::superclock(FormGadgetType* gadgetP, view* in_superview): 
  19.     pane(&gadgetP->rect, in_superview)
  20. {
  21.     reset_idle_timer();
  22.     set_date_timer(0);
  23. }
  24.  
  25. //
  26. // destructor
  27. //
  28. superclock::~superclock() {
  29.     // do nothing
  30. }
  31.  
  32. #pragma mark -
  33.  
  34. // 
  35. // reset_idle_timer()
  36. //
  37. void
  38. superclock::reset_idle_timer() {
  39.     UInt32        now = TimGetSeconds();
  40.     UInt32        next_minute_update = (now/60 + 1) * 60;
  41.     if (now < m_date_timer) {
  42.         m_idle_timer = m_date_timer;
  43.     } else {
  44.         m_idle_timer = next_minute_update;
  45.     }
  46. }
  47.  
  48. // 
  49. // set_idle_timer()
  50. //
  51. void
  52. superclock::set_idle_timer(UInt32 interval) {
  53.     m_idle_timer = TimGetSeconds() + interval;
  54. }
  55.  
  56. // 
  57. // set_date_timer()
  58. //
  59. void
  60. superclock::set_date_timer(UInt32 interval) {
  61.     m_date_timer = TimGetSeconds() + interval;
  62. }
  63.  
  64. #pragma mark -
  65.  
  66. //
  67. // draw_self()
  68. //
  69. void
  70. superclock::draw_self() {
  71.     // variables
  72.     char                        s[timeStringLength] = {0};
  73.     short                        x, y;
  74.     UInt32                    now = TimGetSeconds();
  75.     DateTimeType            date_time;
  76.     
  77.     // calculate positioning
  78.     x = m_bounds.topLeft.x + m_bounds.extent.x;
  79.     y = m_bounds.topLeft.y;
  80.     
  81.     // convert seconds to DateTime
  82.     TimSecondsToDateTime (now, &date_time);
  83.     
  84.     // draw date if date timer is in future
  85.     if (now<m_date_timer) {
  86.         Int16                 months, days, years;
  87.         DateFormatType    format_pref = (DateFormatType) PrefGetPreference(prefDateFormat);
  88.         
  89.         // create date string
  90.         months = date_time.month;
  91.         days = date_time.day;
  92.         years = date_time.year;
  93.         DateToAscii (months, days, years, format_pref, s);
  94.     } else {
  95.         Int16                 hours, minutes;
  96.         TimeFormatType    format_pref = (TimeFormatType) PrefGetPreference(prefTimeFormat);
  97.  
  98.         // create time string
  99.         hours = date_time.hour;
  100.         minutes = date_time.minute;
  101.         TimeToAscii (hours, minutes, format_pref, s);
  102.     }
  103.     
  104.     // draw time string to display
  105.     WinEraseRectangle(&m_bounds, 0);
  106.     // for debugging use:
  107.     // delay(250);
  108.     draw_string(s, x, y, right_align, top_align);
  109.     reset_idle_timer();
  110. }
  111.  
  112. // 
  113. // idle_self()
  114. //
  115. void
  116. superclock::idle_self() {
  117.     UInt32 now = TimGetSeconds();
  118.     
  119.     if (now>=m_idle_timer) {
  120.         draw_self();
  121.     }
  122. }
  123.  
  124. //
  125. // click_self()
  126. //
  127. Boolean
  128. superclock::click_self(int x, int y) {
  129.     UInt32 now = TimGetSeconds();
  130.     
  131.     if (now<m_date_timer) {
  132.         set_date_timer(0);
  133.         // now displaying date, so display time instead
  134.     } else {
  135.         // now displaying time, so display date instead
  136.         set_date_timer(k_date_interval);
  137.     }
  138.     draw_self();
  139.     return true;
  140. }